package JDK8新特性_集合.Stream流的中间方法;

// 目标: 掌握Stream流提供的常见中间方法

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        // 需求1: 找出成绩大于等于60分的数据, 并升序后, 在输出.
        List<Double> list = new ArrayList<>();
        Collections.addAll(list,88.5, 100.0, 60.0, 99.0, 9.5, 99.6, 25.0);
        Stream<Double> stream = list.stream();
        stream.filter(s -> s >= 60).sorted().forEach(s -> System.out.println(s)); // sorted()是把这个Stream<T>对象默认进行升序排序, 并返回新的stream<T>类型

        List<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 26, 172.5);
        Student s2 = new Student("蜘蛛精", 26, 172.5);
        Student s3 = new Student("紫霞", 23, 167.6);
        Student s4 = new Student("白晶晶", 25, 169.0);
        Student s5 = new Student("牛魔王", 35, 183.3);
        Student s6 = new Student("牛夫人", 34, 168.5);
        Collections.addAll(students, s1, s2, s3, s4, s5, s6);
        // 需求2: 找出年龄大于等于23, 且年龄小于等于30岁的学生, 并按照年龄降序输出  sorted(): 默认按照升序排序
        students.stream().filter(s -> s.getAge() >= 23 && s.getAge() <= 30).sorted(((o1, o2) -> o2.getAge() - o1.getAge())).forEach(s -> System.out.println(s));
        // 需求3: 取出身高最高的前三名学生, 并输出   sorted(Comparator<? super T> comparator)  limit(a): 从中取出a个元素返回
        students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(s -> System.out.println(s));
        // 需求4: 取出身高倒数的2名学生, 并输出  skip(a): 跳过a个元素,并把剩下的元素返回
        students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).skip(students.size() - 2).forEach(s -> System.out.println(s));
        // 需求5: 取出身高超过168的学生叫什么名字, 去除重复名字, 再输出
        students.stream().filter(s -> s.getHeight() > 168).map(Student::getName).distinct().forEach(s -> System.out.println(s));
        // distinct() 去重复, 自定义类型的对象(希望内容一样就认为重复, 要重写hashCode,equals方法)

        // concat(Stream a, Stream b)
        Stream<String> stream1 = Stream.of("张三", "李四");
        Stream<String> stream2 = Stream.of("张三2", "李四2", "王五");
        Stream<String> allStream = Stream.concat(stream1,stream2);
        allStream.forEach(System.out::println);
    }
}
